package com.imachen.ymcc.mq.listener;

import com.alibaba.fastjson.JSON;
import com.imachen.ymcc.domain.CourseOrder;
import com.imachen.ymcc.dto.PayDto;
import com.imachen.ymcc.service.ICourseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @description: 课程支付MQ监听器
 * @author: AChen
 * @date: 2022/10/17 0017 : 14:51
 * @version: ver1.0
 * @email
 */
@Component
@Slf4j
@RocketMQTransactionListener(txProducerGroup = "tx-course-order-group")
public class CourseOrderRocketMqListener implements RocketMQLocalTransactionListener {

    @Autowired
    private ICourseOrderService courseOrderService;

    /**
     * 执行本地事务方法
     * @param message
     * @param arg
     * @return
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
        try {
            log.info("课程下单执行本地事务开始！");
            if (arg == null) {
                return RocketMQLocalTransactionState.ROLLBACK;
            }
            CourseOrder courseOrder = JSON.parseObject(arg.toString(), CourseOrder.class);
            // 防止用户下单操作异常,导致线程不安全,可加一个同步锁
            synchronized (courseOrder.getOrderNo()){
                // 防止订单已经保存
                if (courseOrderService.selectOneByOrderNo(courseOrder.getOrderNo()) != null){
                    return RocketMQLocalTransactionState.ROLLBACK;
                }
                // 执行保存订单信息和订单快照信息
                courseOrderService.saveOrderAndItem(courseOrder);
            }
            log.info("课程下单执行本地事务结束！");
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("课程下单执行本地事务异常");
            return RocketMQLocalTransactionState.ROLLBACK;
        }

    }

    /**
     * 执行本地事务回查方法
     * @param message
     * @return
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        try {
            // 将数据转为byte数组
            byte[] bytes = (byte[]) message.getPayload();
            String pram = new String(bytes, StandardCharsets.UTF_8);
            PayDto payDto = JSON.parseObject(pram, PayDto.class);
            if (payDto == null) {
                return RocketMQLocalTransactionState.ROLLBACK;
            }
            CourseOrder courseOrder = courseOrderService.selectOneByOrderNo(payDto.getOrderNo());
            if (courseOrder == null) {
                return RocketMQLocalTransactionState.ROLLBACK;
            }
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }
}
